package cn.wzy.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.wzy.client.AuthClient;
import cn.wzy.domain.Employee;
import cn.wzy.domain.LoginUser;
import cn.wzy.domain.Tenant;
import cn.wzy.exception.ass.ValidUtils;
import cn.wzy.exception.code.ErrorCode;
import cn.wzy.mapper.EmployeeMapper;
import cn.wzy.mapper.TenantMapper;
import cn.wzy.query.TenantQuery;
import cn.wzy.service.ITenantService;
import cn.wzy.util.AjaxResult;
import cn.wzy.util.PageList;
import cn.wzy.util.RedisCache;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wzy
 * @since 2023-02-24
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {

    @Autowired
    private TenantMapper tenantMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private AuthClient authClient;
//    @Autowired
//    private RedisCache redisCache;
    /*
    * 自定义分页
    * */
    @Override
    public PageList selectPageList(TenantQuery query) {
        Page<Tenant> page = new Page<>(query.getPage(),query.getRows());
        List<Tenant> tenantList = tenantMapper.lodePage(page,query);
        PageList pageList = new PageList(page.getTotal(),tenantList);
        return pageList;
    }

    @Override
    public AjaxResult settlement(Tenant tenant) {
//        校验用JSR303
//        判断是否有租户
        List<Tenant> tenants =tenantMapper.selectList(new EntityWrapper<Tenant>().eq("company_name", tenant.getCompanyName()));
//        断言抛异常
        ValidUtils.isEmpty(tenants, ErrorCode.ERROR_CODE_TENANT_EXSIT);
//        判断是否有管理员
        List<Employee> employees = employeeMapper.selectList(new EntityWrapper<Employee>().eq("username", tenant.getEmployee().getUsername()));
//        断言判断
        ValidUtils.isEmpty(employees,ErrorCode.ERROR_CODE_TENANT_ADMIN_EXSIT);
//        al审核
        Boolean isTrue = true;
//        远程调用添加登入表
        Employee employee = tenant.getEmployee();
        LoginUser loginUser = new LoginUser();
        loginUser.setUsername(employee.getUsername());
        loginUser.setTel(employee.getTel());
        loginUser.setEmail(employee.getEmail());
        loginUser.setPassword(employee.getPassword());
        loginUser.setType(1);
        AjaxResult ajaxResult = authClient.addOrUpdate(loginUser);
//        添加租户
        tenant.setRegisterTime(new Date());
        tenant.setState(isTrue?1:0);
        tenant.setAdminId(employee.getId());
        tenantMapper.insert(tenant);
        //        添加员工表
        employee.setInputTime(new Date());
        employee.setLoginId(Long.valueOf(ajaxResult.getResultObj().toString()));
        employee.setType(1);
        employee.setState(0);
        employee.setTenantId(tenant.getId());
        employee.setRealName(employee.getUsername());
        employeeMapper.insert(employee);
//        添加套餐
        tenantMapper.insertMeal(tenant.getId(),tenant.getMealId());
//        发激活码
        if (!isTrue){
            return AjaxResult.me().setSuccess(false).setErrorCode(ErrorCode.ERROR_CODE_400.getCode()).setMessage(ErrorCode.ERROR_CODE_400.getMessage());
        }else {
//            生成验证码，发短信
            String uuid = RandomUtil.randomString(4);
//            redisCache.setCacheObject("activeCode"+tenant.getId(),uuid);
            return AjaxResult.me().setSuccess(true).setErrorCode(ErrorCode.CODE_SUCCEE.getCode()).setMessage(ErrorCode.CODE_SUCCEE.getMessage());
        }
    }
}
